RegEx Rewrites
The regex rewrite is in fact much simpler as there are solid rules of usage. You always work with URIs and the result is always an URI or URL for redirect.
Source is a regex match pattern and Destination is a regex replace pattern. In the destination you can also specify flags.
The whole concept is based on mod_rewrite module for Apache and uses the same syntax.
^/data/(.*) -> http://server/$1 [R]
http://myserver/data/other/?script=value -> http://server/other/?script=value
This would take the string after data, redirect to a different server, but with the selected parameters in place.
You can see you can do some tricks with it. Every () in the regex search pattern can be then used as a variable starting with "$" and index "n":
You can create even more sophisticated rewrites such as:
^/test/(.*)/(.*)$ -> /scripts/$1?value=$2
^/data/(.*)/\?(.*) -> /$1/script.asp?value=$2
This would not do a redirect but a simple internal URI replace. It works even with URL variables and there are no boundaries at all.
If you wish to continue with next rewrite, specify the flags without [L].
Also, there is a special destination "-" which means not to replace anything. It might come handy sometimes.
You may also want to rewrite e. g. http://www.icewarp.com.br/comprar to http://www.icewarp.com.br/purchase.
You can set a non-regex rewrite, but it will fail in the case, someone writes http://www.icewarp.com.br/comprar/ for a server it is the same location, but it is not the same string.
Regex rewrite can help:
Source: ^/comprar
Destination: /purchase [R]
The rest is up to admins - look for mod_rewrite syntax for more details.
Flags
With these flags the admin can gain complete control of <WEB> behavior.
Flags need to be separated from the regex with space and surrounded in "[ ]" brackets. Such as:
[L,R]
Available flags are:
Field |
Description |
---|---|
[R]edirect |
redirect instead of rewrite |
[L]ast |
do not process other rewrite this is the last one |
[F]orbidden |
the user will receive 403 Forbidden message when accessed the URL |
[C]hain |
if the rule is not matched, skip all following rules containing [C] flag |
[RP] |
see lower the Reverse Proxy section |
[V=VARNAME] |
match to server variable instead of URI |
[] (void flag) |
force processing following rules |
Note: If no flag is specified, the default flag is [L]. If rewrite is matched no other rule will be processed, unless you specify void flag []. The behavior is the same for non regex rewrites (redirects).
Server Varibles
Using the [V=] flag you can achieve some sophisticated URI rewrite functionality. Instead of the URI string, the value of the server variable will be matched. Use with [C] flags and usually without the URI rewrite- thus with "-" for destination only.
Supported variables are the general HTTP_* variables: HTTP_HOST, HTTP_REFERER, HTTP_USER_AGENT, THE_REQUEST, REMOTE_IP and certificate specific CERT_ and CERT_SERVER variables: CERT_SUBJECT, CERT_ISSUER, CERT_FLAGS, CERT_SERVER_SUBJECT, CERT_SERVER_ISSUER, CERT_SERVER_FLAGS. If used will require and verify peer certificate authentication can be based only on the client certificate using CERT_SUBJECT.
Virtual host is checked for "www.myhost.com".
The V= flag will be usually used with the [C] chained flag as a predecessor, such as in the following rewrite rule.
^/webmail/ - [C]
^Lynx/ /webmail/basic/[V=HTTP_USER_AGENT,C,R]
The example above would match the /webmail/ in the URL (not replace anything) then it would check if the HTTP_USER_AGENT contains Lynx/ and if it does, redirect to /mail/. Lynx web browser simply cannot go to /webmail/ and will be redirected to /mail/
Very flexible!
Reverse Proxy
Reverse Proxy serves for hiding another webserver (usually in local networks) and dynamically rewriting all links that would point to the hidden webserver into links that point to the reverse proxy itself.
Example of rewrite rule setting:
$/proxy/.*^
http://server/uri/$1 [L,P,RP=/proxy/=http://server/uri/\]
When a user goes to /proxy/* URI, the content is loaded from "hidden" server http://server/uri/*
The hidden server may refer to itself in the output HTML code. The definition of RP=/proxy/=http://server/uri/ causes all links pointing to http://server/uri/* to be dynamically rewritten with /proxy/* URI, so hidden server will be effectively hidden.